﻿#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

// 如果编译器不支持 _USE_MATH_DEFINES，手动定义 M_PI
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

// 声明重载的函数
double calculateArea(const string& shape, double size = 1.0);
double calculateArea(const string& shape, double length, double width);
double calculateArea(const string& shape, initializer_list<double> sides);

// 计算圆形的面积
double calculateArea(const string& shape, double radius) {
	if (shape != "circle") {
		throw invalid_argument("Shape must be 'circle'");
	}
	return M_PI * radius * radius;
}

// 计算矩形的面积
double calculateArea(const string& shape, double length, double width) {
	if (shape != "rectangle") {
		throw invalid_argument("Shape must be 'rectangle'");
	}
	return length * width;
}

// 计算三角形的面积（使用海伦公式）
double calculateArea(const string& shape, initializer_list<double> sides) {
	if (shape != "triangle") {
		throw invalid_argument("Shape must be 'triangle'");
	}
	double sum = 0.0;
	for (auto side : sides) {
		sum += side;
	}
	double s = sum / 2;
	double area = sqrt(s * (s - *sides.begin()) * (s - *(next(sides.begin(), 1))) * (s - *(next(sides.begin(), 2))));
	return area;
}

int main() {
	int choice;
	cout << "请输入要计算的图形类型(1-圆形，2-矩形，3-三角形): ";
	cin >> choice;

	double radius, length, width, a, b, c;

	switch (choice) {
	case 1:
		cout << "请输入圆的半径: ";
		cin >> radius;
		cout << "该图形为圆形，其面积为：" << calculateArea("circle", radius) << endl;
		break;
	case 2:
		cout << "请输入矩形的长和宽: ";
		cin >> length >> width;
		cout << "该图形为矩形，其面积为：" << calculateArea("rectangle", length, width) << endl;
		break;
	case 3:
		cout << "请输入三角形的三条边长: ";
		cin >> a >> b >> c;
		cout << "该图形为三角形，其面积为：" << calculateArea("triangle", { a, b, c }) << endl;
		break;
	default:
		cout << "无效的图形类型。" << endl;
	}

	return 0;
}